package com.screen.screendisplay.service.impl.al;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.screen.screendisplay.entity.al.AlOpermonEqpState;
import com.screen.screendisplay.mapper.al.AlOpermonEqpStateMapper;
import com.screen.screendisplay.service.al.AlOpermonEqpStateService;
import com.screen.screendisplay.vo.al.AlOpermonEquipmentStateVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备状态表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class AlOpermonEqpStateServiceImpl extends ServiceImpl<AlOpermonEqpStateMapper, AlOpermonEqpState> implements AlOpermonEqpStateService {

    @Override
    public List<AlOpermonEquipmentStateVO> getLatestEquipmentState() {
        // 查询所有最新的设备状态（按年月日倒序）
        QueryWrapper<AlOpermonEqpState> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("year", "month", "day");
        queryWrapper.orderByAsc("aluminum_plant_type");
        
        List<AlOpermonEqpState> stateList = baseMapper.selectList(queryWrapper);
        
        if (stateList == null || stateList.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 转换为VO对象列表
        List<AlOpermonEquipmentStateVO> voList = new ArrayList<>();
        
        for (AlOpermonEqpState state : stateList) {
            AlOpermonEquipmentStateVO vo = new AlOpermonEquipmentStateVO();
            
            // 根据铝厂类型设置铝厂名称
            String plantName = getPlantName(state.getAluminumPlantType());
            vo.setPlantName(plantName);
            
            // 设置设备状态数据
            vo.setElectrolyticCellStopped(state.getElectrolyticCellStopped());
            vo.setElectrolyticCellRunning(state.getElectrolyticCellRunning());
            vo.setRectifierUnitStopped(state.getRectifierUnitStopped());
            vo.setRectifierUnitRunning(state.getRectifierUnitRunning());
            
            voList.add(vo);
        }
        
        return voList;
    }
    
    /**
     * 根据铝厂类型获取铝厂名称
     *
     * @param aluminumPlantType 铝厂类型：0-霍煤鸿铝厂,1-扎哈淖尔铝厂,2-白音华铝厂
     * @return 铝厂名称
     */
    private String getPlantName(Byte aluminumPlantType) {
        if (aluminumPlantType == null) {
            return "未知铝厂";
        }
        
        switch (aluminumPlantType) {
            case 0:
                return "霍煤鸿铝厂";
            case 1:
                return "扎哈淖尔铝厂";
            case 2:
                return "白音华铝厂";
            default:
                return "未知铝厂";
        }
    }

}
